package com.ruoyi.violate.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.violate.domain.ViolateTilte;
import com.ruoyi.web.controller.common.PageReadListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.violate.mapper.ViolateTypeShopMapper;
import com.ruoyi.violate.domain.ViolateTypeShop;
import com.ruoyi.violate.service.IViolateTypeShopService;

/**
 * 同类型店铺Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-29
 */
@Service
@Slf4j
public class ViolateTypeShopServiceImpl implements IViolateTypeShopService 
{
    @Autowired
    private ViolateTypeShopMapper violateTypeShopMapper;

    /**
     * 查询同类型店铺
     * 
     * @param id 同类型店铺主键
     * @return 同类型店铺
     */
    @Override
    public ViolateTypeShop selectViolateTypeShopById(Long id)
    {
        return violateTypeShopMapper.selectViolateTypeShopById(id);
    }

    /**
     * 查询同类型店铺列表
     * 
     * @param violateTypeShop 同类型店铺
     * @return 同类型店铺
     */
    @Override
    public List<ViolateTypeShop> selectViolateTypeShopList(ViolateTypeShop violateTypeShop)
    {
        return violateTypeShopMapper.selectViolateTypeShopList(violateTypeShop);
    }


    @Override
    public List<String> selectViolateTypeShopNames()
    {
        ViolateTypeShop violateTypeShop = new ViolateTypeShop();
        List<ViolateTypeShop> violateTypeShops = violateTypeShopMapper.selectViolateTypeShopList(violateTypeShop);
        Set<String> shopNames = new HashSet<>();
        for (ViolateTypeShop typeShop : violateTypeShops) {
            if (typeShop.getShopName() ==null){
                continue;
            }
            shopNames.add(typeShop.getShopName());
        }
        List<String>list = new ArrayList<String>(shopNames);

        return list ;
    }

    /**
     * 新增同类型店铺
     * 
     * @param violateTypeShop 同类型店铺
     * @return 结果
     */
    @Override
    public int insertViolateTypeShop(ViolateTypeShop violateTypeShop)
    {
        violateTypeShop.setCreateTime(DateUtils.getNowDate());
        return violateTypeShopMapper.insertViolateTypeShop(violateTypeShop);
    }

    /**
     * 修改同类型店铺
     * 
     * @param violateTypeShop 同类型店铺
     * @return 结果
     */
    @Override
    public int updateViolateTypeShop(ViolateTypeShop violateTypeShop)
    {
        violateTypeShop.setUpdateTime(DateUtils.getNowDate());
        return violateTypeShopMapper.updateViolateTypeShop(violateTypeShop);
    }

    /**
     * 批量删除同类型店铺
     * 
     * @param ids 需要删除的同类型店铺主键
     * @return 结果
     */
    @Override
    public int deleteViolateTypeShopByIds(Long[] ids)
    {
        return violateTypeShopMapper.deleteViolateTypeShopByIds(ids);
    }

    /**
     * 删除同类型店铺信息
     * 
     * @param id 同类型店铺主键
     * @return 结果
     */
    @Override
    public int deleteViolateTypeShopById(Long id)
    {
        return violateTypeShopMapper.deleteViolateTypeShopById(id);
    }


    /**
     * 导入shop
     * @param filePath
     * @return
     */
    @Override
    public int importFile(String filePath) throws IOException {

        String path = RuoYiConfig.getProfile() + filePath.substring(8, filePath.length());
        System.out.println(path);

        List<ViolateTypeShop> list = new ArrayList<ViolateTypeShop>();
        EasyExcel.read(path, ViolateTypeShop.class, new PageReadListener<ViolateTypeShop>(dataList -> {
            for (ViolateTypeShop violateTypeShop : dataList) {
                log.info("读取到一条数据{}", JSON.toJSONString(violateTypeShop));
                list.add(violateTypeShop) ;
            }
        })).sheet().doRead();

        String id = IdUtils.simpleUUID() ;
        for (ViolateTypeShop violateTypeShop : list) {
            this.insertViolateTypeShop(violateTypeShop) ;
            log.info("添加数据{}",violateTypeShop);
        }
        return 1;
    }
}
